<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content=
    "application/xhtml+xml; charset=iso-8859-1" />
    <title>
      5.10.&nbsp;GCC-6.3.0 - Pass 2
    </title>
    <link rel="stylesheet" type="text/css" href="../stylesheets/lfs.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="stylesheet" href="../stylesheets/lfs-print.css" type=
    "text/css" media="print" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body class="lfs" id="lfs-8.0">
    <div class="navheader">
      <h4>
        Linux From Scratch - Version 8.0
      </h4>
      <h3>
        Chapter&nbsp;5.&nbsp;Constructing a Temporary System
      </h3>
      <ul>
        <li class="prev">
          <a accesskey="p" href="binutils-pass2.html" title=
          "Binutils-2.27 - Pass 2">Prev</a>
          <p>
            Binutils-2.27 - Pass 2
          </p>
        </li>
        <li class="next">
          <a accesskey="n" href="tcl.html" title="Tcl-core-8.6.6">Next</a>
          <p>
            Tcl-core-8.6.6
          </p>
        </li>
        <li class="up">
          <a accesskey="u" href="chapter05.html" title=
          "Chapter&nbsp;5.&nbsp;Constructing a Temporary System">Up</a>
        </li>
        <li class="home">
          <a accesskey="h" href="../index.html" title=
          "Linux From Scratch - Version 8.0 ">Home</a>
        </li>
      </ul>
    </div>
    <div class="wrap" lang="en" xml:lang="en">
      <h1 class="sect1">
        <a id="ch-tools-gcc-pass2" name="ch-tools-gcc-pass2"></a>5.10.
        GCC-6.3.0 - Pass 2
      </h1>
      <div class="package" lang="en" xml:lang="en">
        <p>
          The GCC package contains the GNU compiler collection, which
          includes the C and C++ compilers.
        </p>
        <div class="segmentedlist">
          <div class="seglistitem">
            <div class="seg">
              <strong class="segtitle">Approximate build time:</strong>
              <span class="segbody">11 SBU</span>
            </div>
            <div class="seg">
              <strong class="segtitle">Required disk space:</strong>
              <span class="segbody">2.9 GB</span>
            </div>
          </div>
        </div>
      </div>
      <div class="installation" lang="en" xml:lang="en">
        <h2 class="sect2">
          5.10.1. Installation of GCC
        </h2>
        <p>
          Our first build of GCC has installed a couple of internal system
          headers. Normally one of them, <code class=
          "filename">limits.h</code>, will in turn include the corresponding
          system <code class="filename">limits.h</code> header, in this case,
          <code class="filename">/tools/include/limits.h</code>. However, at
          the time of the first build of gcc <code class=
          "filename">/tools/include/limits.h</code> did not exist, so the
          internal header that GCC installed is a partial, self-contained
          file and does not include the extended features of the system
          header. This was adequate for building the temporary libc, but this
          build of GCC now requires the full internal header. Create a full
          version of the internal header using a command that is identical to
          what the GCC build system does in normal circumstances:
        </p>
        <pre class="userinput">
<kbd class="command">cat gcc/limitx.h gcc/glimits.h gcc/limity.h &gt; \
  `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h</kbd>
</pre>
        <p>
          Once again, change the location of GCC's default dynamic linker to
          use the one installed in <code class="filename">/tools</code>.
        </p>
        <pre class="userinput">
<kbd class="command">for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&amp;@g' \
      -e 's@/usr@/tools@g' $file.orig &gt; $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' &gt;&gt; $file
  touch $file.orig
done</kbd>
</pre>
        <p>
          If building on x86_64, change the default directory name for 64-bit
          libraries to <span class="quote">&ldquo;<span class=
          "quote">lib</span>&rdquo;</span>:
        </p>
        <pre class="userinput">
<kbd class="command">case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
  ;;
esac</kbd>
</pre>
        <p>
          As in the first build of GCC it requires the GMP, MPFR and MPC
          packages. Unpack the tarballs and move them into the required
          directory names:
        </p>
        <pre class="userinput">
<kbd class="command">tar -xf ../mpfr-3.1.5.tar.xz
mv -v mpfr-3.1.5 mpfr
tar -xf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2 gmp
tar -xf ../mpc-1.0.3.tar.gz
mv -v mpc-1.0.3 mpc</kbd>
</pre>
        <p>
          Create a separate build directory again:
        </p>
        <pre class="userinput">
<kbd class="command">mkdir -v build
cd       build</kbd>
</pre>
        <p>
          Before starting to build GCC, remember to unset any environment
          variables that override the default optimization flags.
        </p>
        <p>
          Now prepare GCC for compilation:
        </p>
        <pre class="userinput">
<kbd class="command">CC=$LFS_TGT-gcc                                    \
CXX=$LFS_TGT-g++                                   \
AR=$LFS_TGT-ar                                     \
RANLIB=$LFS_TGT-ranlib                             \
../configure                                       \
    --prefix=/tools                                \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --enable-languages=c,c++                       \
    --disable-libstdcxx-pch                        \
    --disable-multilib                             \
    --disable-bootstrap                            \
    --disable-libgomp</kbd>
</pre>
        <div class="variablelist">
          <p class="title">
            <strong>The meaning of the new configure options:</strong>
          </p>
          <dl class="variablelist">
            <dt>
              <span class="term"><em class=
              "parameter"><code>--enable-languages=c,c++</code></em></span>
            </dt>
            <dd>
              <p>
                This option ensures that both the C and C++ compilers are
                built.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--disable-libstdcxx-pch</code></em></span>
            </dt>
            <dd>
              <p>
                Do not build the pre-compiled header (PCH) for <code class=
                "filename">libstdc++</code>. It takes up a lot of space, and
                we have no use for it.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--disable-bootstrap</code></em></span>
            </dt>
            <dd>
              <p>
                For native builds of GCC, the default is to do a "bootstrap"
                build. This does not just compile GCC, but compiles it
                several times. It uses the programs compiled in a first round
                to compile itself a second time, and then again a third time.
                The second and third iterations are compared to make sure it
                can reproduce itself flawlessly. This also implies that it
                was compiled correctly. However, the LFS build method should
                provide a solid compiler without the need to bootstrap each
                time.
              </p>
            </dd>
          </dl>
        </div>
        <p>
          Compile the package:
        </p>
        <pre class="userinput">
<kbd class="command">make</kbd>
</pre>
        <p>
          Install the package:
        </p>
        <pre class="userinput">
<kbd class="command">make install</kbd>
</pre>
        <p>
          As a finishing touch, create a symlink. Many programs and scripts
          run <span class="command"><strong>cc</strong></span> instead of
          <span class="command"><strong>gcc</strong></span>, which is used to
          keep programs generic and therefore usable on all kinds of UNIX
          systems where the GNU C compiler is not always installed. Running
          <span class="command"><strong>cc</strong></span> leaves the system
          administrator free to decide which C compiler to install:
        </p>
        <pre class="userinput">
<kbd class="command">ln -sv gcc /tools/bin/cc</kbd>
</pre>
        <div class="admon caution">
          <img alt="[Caution]" src="../images/caution.png" />
          <h3>
            Caution
          </h3>
          <p>
            At this point, it is imperative to stop and ensure that the basic
            functions (compiling and linking) of the new toolchain are
            working as expected. To perform a sanity check, run the following
            commands:
          </p>
          <pre class="userinput">
<kbd class="command">echo 'int main(){}' &gt; dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'</kbd>
</pre>
          <p>
            If everything is working correctly, there should be no errors,
            and the output of the last command will be of the form:
          </p>
          <pre class="screen">
<code class=
"computeroutput">[Requesting program interpreter: /tools/lib/ld-linux.so.2]</code>
</pre>
          <p>
            Note that <code class="filename">/tools/lib</code>, or
            <code class="filename">/tools/lib64</code> for 64-bit machines
            appears as the prefix of the dynamic linker.
          </p>
          <p>
            If the output is not shown as above or there was no output at
            all, then something is wrong. Investigate and retrace the steps
            to find out where the problem is and correct it. This issue must
            be resolved before continuing on. First, perform the sanity check
            again, using <span class="command"><strong>gcc</strong></span>
            instead of <span class="command"><strong>cc</strong></span>. If
            this works, then the <code class="filename">/tools/bin/cc</code>
            symlink is missing. Install the symlink as per above. Next,
            ensure that the <code class="envar">PATH</code> is correct. This
            can be checked by running <span class="command"><strong>echo
            $PATH</strong></span> and verifying that <code class=
            "filename">/tools/bin</code> is at the head of the list. If the
            <code class="envar">PATH</code> is wrong it could mean that you
            are not logged in as user <code class="systemitem">lfs</code> or
            that something went wrong back in <a class="xref" href=
            "../chapter04/settingenvironment.html" title=
            "4.4.&nbsp;Setting Up the Environment">Section&nbsp;4.4,
            &ldquo;Setting Up the Environment.&rdquo;</a>
          </p>
          <p>
            Once all is well, clean up the test files:
          </p>
          <pre class="userinput">
<kbd class="command">rm -v dummy.c a.out</kbd>
</pre>
        </div>
      </div>
      <div class="content" lang="en" xml:lang="en">
        <p>
          Details on this package are located in <a class="xref" href=
          "../chapter06/gcc.html#contents-gcc" title=
          "6.17.2.&nbsp;Contents of GCC">Section&nbsp;6.17.2, &ldquo;Contents
          of GCC.&rdquo;</a>
        </p>
      </div>
    </div>
    <div class="navfooter">
      <ul>
        <li class="prev">
          <a accesskey="p" href="binutils-pass2.html" title=
          "Binutils-2.27 - Pass 2">Prev</a>
          <p>
            Binutils-2.27 - Pass 2
          </p>
        </li>
        <li class="next">
          <a accesskey="n" href="tcl.html" title="Tcl-core-8.6.6">Next</a>
          <p>
            Tcl-core-8.6.6
          </p>
        </li>
        <li class="up">
          <a accesskey="u" href="chapter05.html" title=
          "Chapter&nbsp;5.&nbsp;Constructing a Temporary System">Up</a>
        </li>
        <li class="home">
          <a accesskey="h" href="../index.html" title=
          "Linux From Scratch - Version 8.0 ">Home</a>
        </li>
      </ul>
    </div>
  </body>
</html>
